<template>
	<view class="bg">
		<view class="big_write_amount">
			<view class="big_write_amount_top">
				<view class="big_write_amount_text">
					大写金额：
				</view>
				<view class="copy" @click="copy">
					复制
				</view>
			</view>
			<view class="big_write_amount_area">
				{{bigAmount}}
			</view>
		</view>
		<view class="small_amount">
			<view class="small_amount_text">
				小写金额：
			</view>
			<view class="small_amount_input">
				<input type="text" placeholder="请输入小写金额" v-model="smallAmount" @input="reverse">
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bigAmount: '', // 大写金额
				smallAmount: '', // 小写金额
			}
		},
		onLoad() {
			// 加载分享菜单
			this.loadingShareMenu();
		},
		methods: {
			// 输入框输入实时转换
			reverse() {
				if (this.smallAmount == '' || this.smallAmount == '0') {
					uni.showToast({ //显示对话框
						title: "请输入大于0的小写金额",
						icon: 'none',
						duration: 1000,
					})
				} else {
					var smallAmountNumber = Number(this.smallAmount);
					console.log(smallAmountNumber);
					if (isNaN(smallAmountNumber)) {
						uni.showToast({ //显示对话框
							title: "请输入大于0的数字",
							icon: 'none',
							duration: 1000,
						})
					} else {
						this.smallAmountToBigAmount(smallAmountNumber);
					}
				}
			},
			// 转换方法
			smallAmountToBigAmount(number) {
				let ret = ''
				if (number !== '' && number != null && number !== '0') {
					let unit = '仟佰拾亿仟佰拾万仟佰拾元角分'
					let str = ''
					number += '00'
					const point = number.indexOf('.')
					if (point >= 0) {
						number = number.substring(0, point) + number.substr(point + 1, 2)
					}
					unit = unit.substr(unit.length - number.length)
					for (let i = 0; i < number.length; i++) {
						str +=
							'零壹贰叁肆伍陆柒捌玖'.charAt(number.charAt(i)) + unit.charAt(i)
					}
					ret =
						str
						.replace(/零(仟|佰|拾|角)/g, '零')
						.replace(/(零)+/g, '零')
						.replace(/零(万|亿|元)/g, '$1')
						.replace(/(亿)万|(拾)/g, '$1$2')
						.replace(/^元零?|零分/g, '')
						.replace(/元$/g, '元') + '整'
				}
				if (ret == '分整') {
					ret = '';
				}
				this.bigAmount = ret;
			},
			// 复制大写金额到剪切板
			copy(){
				uni.setClipboardData({
					data: this.bigAmount,
					showToast: true,
					success(res) {
						console.log(res);
					}
				})
			},
			// 加载分享菜单
			loadingShareMenu() {
				wx.showShareMenu({
					withShareTicket: true,
					//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
					menus: ["shareAppMessage", "shareTimeline"]
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	@import url('../../static/css/basic.css');

	.big_write_amount {
		width: 90%;
		margin-left: auto;
		margin-right: auto;
		padding-top: 20px;

		.big_write_amount_top {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.big_write_amount_text {
				font-size: 20px;
				font-weight: 600;
				color: #55557f;
			}

			.copy {
				font-weight: 18px;
				color: #a8a8fa;
			}
		}

		.big_write_amount_area {
			margin-top: 20px;
			width: 100%;
			height: 200rpx;
			border: 2px dashed #00e2e2;
			background-color: #eff4fc;
			font-size: 25px;
			color: #009d9d;
		}
	}

	.small_amount {
		width: 90%;
		margin-left: auto;
		margin-right: auto;
		margin-top: 100rpx;

		.small_amount_text {
			font-size: 20px;
			font-weight: 600;
			color: #55557f;
		}

		.small_amount_input {
			margin-top: 30rpx;
			width: 100%;

			input {
				width: 90%;
				height: 100rpx;
				border-radius: 10rpx;
				border: 1px solid #5678ab;
				padding-left: 20rpx;
				color: #68689c;
				font-size: 20px;
			}
		}
	}
</style>
